package com.eascs.admin.o2o.receivedRecords.controller;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.alibaba.dubbo.config.annotation.Reference;
import com.eascs.admin.common.utils.StringUtils;
import com.eascs.admin.o2o.loan.action.O2oSearchCustemer;
import com.eascs.admin.o2o.receivedRecords.entity.ReceivedRecordsEntity;
import com.eascs.admin.o2o.receivedRecords.entity.ReceivedRecordsVo;
import com.eascs.admin.o2o.receivedRecords.service.ReceivedRecordsService;
import com.eascs.app.log.Logger;
import com.eascs.app.log.LoggerFactory;
import com.eascs.common.util.StringUtil;
import com.eascs.site.controller.BaseRestController;
import com.eascs.site.view.BaseRestResult;
import com.eascs.site.view.HtmlRestResult;
import com.eascs.site.view.ViewContext;
import com.eascs.tag.MenuTag;
import com.eascs.web.sso.UserHolder;
import com.eascs.workflow.common.service.sys.SysOrgService;
import com.eascs.workflow.common.service.sys.SysPermissionService;
import com.eascs.workflow.common.vo.ModelResult;

@RestController
@RequestMapping("/rReceived")
public class ReceivedRecordsRestController extends BaseRestController {
	private static Logger log = LoggerFactory.getLogger(ReceivedRecordsRestController.class);
	@Autowired
	private ReceivedRecordsService service;
	@Reference
	SysOrgService sysOrgService;
	@Reference
	SysPermissionService sysPermissionService;

	@RequestMapping("/add_next")
	public BaseRestResult<String> updateRecord(String billCode) {
		if (StringUtils.isEmpty(billCode)) {// 获取贷款单号
			return BaseRestResult.warn("贷款单号为空，请输入");// 待添加提示对话框----------------------------
		}
		ReceivedRecordsVo vo = service.findReceivedRecordsEntity(billCode);
		if (vo != null) {
			ViewContext.put("vo", vo);
			return HtmlRestResult.popHtmlFragment("/receivedRecords/update_info");
		} else {
			return BaseRestResult.warn("该贷款单号不存在");
		}
	}

	@RequestMapping("/add_save")
	public BaseRestResult<String> add_save(ReceivedRecordsEntity entity) {
		entity.setCreatorName(UserHolder.getUser().getCname());// 创建人name
		entity.setCreatorId(UserHolder.getUser().getNo());// 创建人id
		entity.setCreateDate(new Date());// 创建日期
		entity.setIsDel("N");
		if(entity.getReceivedDate()!=null){
			if(entity.getReceivedAmount()!=null){
				if(entity.getUpdateName()!=null){
					if(entity.getCoefficient()!=null){
					}else{return BaseRestResult.error("请确保所有必填项都已填写!");}
				}else{return BaseRestResult.error("请确保所有必填项都已填写!");}
			}else{return BaseRestResult.error("请确保所有必填项都已填写!");}
		}else{return BaseRestResult.error("请确保所有必填项都已填写!");}
		int after = service.insertRecords(entity);
		if (after > 0) {
			return BaseRestResult.info("保存成功").andRedirect("/receivedRecords/index");
		} else {
			return BaseRestResult.error("保存失败");
		}
	}

	/**
	 * 软删除
	 * 
	 * @return
	 */
	@RequestMapping("/del")
	public BaseRestResult<String> delRecord(String id) {
		boolean flag = false;
		BaseRestResult<String> result = null;
		// entity.setIsDel("Y");// 设置删除标记为Y
		// entity.setCreatorName(UserHolder.getUser().getCname());// 设置操作人
		// entity.setCreateDate(new Date());// 设置操作时间(最后更新时间)
		// entity.setCreatorId(UserHolder.getUser().getNo());// 设置操作时间(最后更新时间)
		flag = service.deleteHKJL(id);
		if (flag == true) {
			result = BaseRestResult.info("删除成功").andRefresh();
		} else if (flag == false) {
			result = BaseRestResult.error("删除失败");
		}
		return result;
	}

	/**
	 * name联想380金服全体成员
	 **/
	@RequestMapping(value = "/autocomplete_person")
	public BaseRestResult<List<Object[]>> customer(O2oSearchCustemer o2oSearchCustemer) {
		List<Object[]> data = new ArrayList<>();
		if (StringUtil.hasText(o2oSearchCustemer.getName())) {
			ModelResult<List<Object[]>> da = sysPermissionService.getOperatorList(o2oSearchCustemer.getName());
			data = da.getModel();
		}
		return BaseRestResult.buildRestResult(data);
	}

}
